package xyz.lwm.lazycat.transaction.policy;

import xyz.lwm.lazycat.transaction.execute.TranDefinition;
import xyz.lwm.lazycat.transaction.execute.Transaction;
import xyz.lwm.lazycat.transaction.execute.TransactionManager;

/**
 * 以非事务的方式运行，若存在当前事务，则暂停（挂起）当前事务
 *
 * @author lwm
 * @see xyz.lwm.lazycat.transaction.execute.Propagation#NOT_SUPPORTED
 */
class NotSupportedPolicy extends AbstractPolicy {

    public NotSupportedPolicy(TranDefinition tranDefinition) {
        super(tranDefinition);
    }

    @Override
    public <T> T execute(CheckedSupplier<T> supplier) throws Throwable {
        Transaction current = TransactionManager.current();
        if (current.isOpen()) {
            TransactionManager.suspend();
        }
        try {
            return supplier.get();
        } finally {
            if (current.isOpen()) {
                TransactionManager.resume(current);
            }
        }
    }

}
